package ar.uba.fi.tonyvaliente.signature;

import ar.uba.fi.tonyvaliente.utils.MD5;

/**
 * Implementacion de la funcion de hash MD5
 */
public class MD5HashFunction extends HashFunction {

	public MD5HashFunction(int length) {
		super(length);
	}

	@Override
	public Number hash(String data) {
		String hash = MD5.calculateHash(data);
		Long hashValue = 0L;
		
		StringBuffer value = new StringBuffer();
		
		byte[] bytes = hash.getBytes();

		for (int i = 0, j = 0; i < bytes.length && j < 6; i++, j++) {
		
			byte b = bytes[i];
			value.append(Math.abs(b));
		}

		hashValue = new Long(value.toString());
		return Math.abs(hashValue%this.getLength());
	}

}
